package com.wc.算法基础课.E第五讲动态规划.状压DP.毕业旅行问题;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/27 11:48
 * @description https://www.acwing.com/problem/content/733/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 20, M = 1 << N;
    // f[i][j] 表示 i 的二进制为1的已经遍历完了，最后到达的是j点的最小花销
    static int[][] f = new int[M][N];
    static int[][] w = new int[N][N];
    static int INF = 0x3f3f3f3f;
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                w[i][j] = sc.nextInt();
        for (int i = 0; i < 1 << n; i++) {
            Arrays.fill(f[i], 0, n, INF);
        }
        // 初始位置,是1 号点是 1 的时候花销是0
        f[1][0] = 0;
        // 所有状态第一次都得包含 1 号位置的数
        for (int i = 1; i < 1 << n; i += 2) {
            for (int j = 0; j < n; j++) {
                // 最终为j的一定是包括j的
                if ((i >> j & 1) == 1) {
                    // 前一位为末尾为k,那说明前一位也得包含k
                    for (int k = 0; k < n; k++) {
                        // 这里 i - (1 << j) 去除掉第j位，但是可以到达地k位的，然后i是既要包括k又要包括j的才行,去除自己走自己的路径
                        if (((i - (1 << j) >> k) & 1) == 1) {
                            f[i][j] = Math.min(f[i][j], f[i - (1 << j)][k] + w[k][j]);
                        }
                    }
                }
            }
        }
        // 最后遍历，全部到达后最后回到1的位置的滆湖
        int res = INF;
        for (int i = 0; i < n; i++) {
            res = Math.min(res, f[(1 << n) - 1][i] + w[i][0]);
        }
        out.println(res);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
